//
//  PKTopicDetailViewController.m
//  PianKe
//
//  Created by lanou on 15/10/18.
//  Copyright (c) 2015年 李伟伟. All rights reserved.
//

#import "PKTopicDetailViewController.h"
#import "PKTopicDetailModel.h"
#import "PKTopicCommentModel.h"
#import "PKTopicCommentCell.h"
#import "PKRequestManager.h"
#import "NSString+Html.h"

#import "PKTopicCommentHeadView.h"

@interface PKTopicDetailViewController ()<UIWebViewDelegate,UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *topicCommentArray;
@property(nonatomic,strong)PKTopicDetailModel *topicDetailModel;
@property(nonatomic,strong)PKTopicCommentModel *topicCommentModel;
//@property(nonatomic,strong)UIWebView *webView;
@property(nonatomic,assign)CGFloat Y;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)PKTopicCommentHeadView *headView;

@property(nonatomic,strong)UILabel *GroupTitleLabel;
@property(nonatomic,strong)UILabel *useerNameLabel;
@property(nonatomic,strong)UILabel *articleTitleLabel;
@property(nonatomic,strong)UIImageView *unameLabel;

@end

@implementation PKTopicDetailViewController

- (NSMutableArray *)topicCommentArray

{
    if (_topicCommentArray == nil) {
        self.topicCommentArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _topicCommentArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    _headView = [[PKTopicCommentHeadView alloc]init];
    
    _headView.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 200, SCREEN_WITDTH, 100)];
    _headView.webView.delegate = self;
    _headView.webView.backgroundColor = [UIColor blackColor];
    [self.view addSubview:_headView];
  
    [self requestData];
    
    
//    self.webView.scrollView
    
}

-(void)requestData
{
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"contentid"]= _contentid;
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"client"] = @"1";
    parDic[@"version"] = @"3.0.2";
    parDic[@"auth"] = @"Wc06FCrkoq1DCMVzGMTikDJxQ8bm3Mrm2NpT9qWjwzcWP23tBKQx1c4P0";
    
    NSLog(@"-------------%@",_contentid);
    
    
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/group/posts_info" parDic:parDic finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];
        NSDictionary *infoDic = dic[@"data"][@"postsinfo"];
        _topicDetailModel = [[PKTopicDetailModel alloc]init];
        [_topicDetailModel setValuesForKeysWithDictionary:infoDic];
        [_headView setConfigreModel:_topicDetailModel];

//        NSString *htmlString = [NSString importStyleWithHtmlString:_topicDetailModel.html];
//        [_headView.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:[NSBundle mainBundle].bundlePath]];
        
        NSArray *commentArray = dic[@"data"][@"commentlist"];
        for (NSDictionary *commentDic in commentArray) {
            _topicCommentModel = [[PKTopicCommentModel alloc]init];
            [_topicCommentModel setValuesForKeysWithDictionary:commentDic];
            
            [self.topicCommentArray addObject:_topicCommentModel];
          
        }
        
        [self.tableView reloadData];
        
    } error:^(NSError *error) {
        NSLog(@"----------------社区错误-------------");
    }];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{

    _Y = [[_headView.webView stringByEvaluatingJavaScriptFromString:@"document.body.scrollHeight"] floatValue];
    _headView.webView.frame = CGRectMake(0, 150, SCREEN_WITDTH, _Y);

    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 150, SCREEN_WITDTH, SCREEN_HEIGHT - 70 - 100) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableHeaderView = _headView;
    [self.tableView registerClass:[PKTopicCommentCell class] forCellReuseIdentifier:@"comment"];

    [self.view addSubview:_tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"%ld",_topicCommentArray.count);
    return _topicCommentArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKTopicCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"comment" forIndexPath:indexPath];
    [cell configureTopicCommentCell:_topicCommentArray[indexPath.row]];
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%@",_topicCommentArray);
    return 60 + [PKTopicCommentCell heightOfCommentString:_topicCommentArray[indexPath.row]];
    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
